using System;
using System.Collections.Generic;
using System.Text;

namespace IWMAG.Helpers {
    public class IndexList<T> : IEnumerable<T> {
        internal List<Tuple<bool, T>> Data = new List<Tuple<bool, T>>();

        public int Count {
            get { return Data.Count; }
        }

        public T this[int index]
        {
            get
            {
                return Data[index].Item2;
            }
        }

        public IndexList() {}

        public IndexList(params T[] data) {
            foreach (T item in data)
                Add(item);
        }

        public IndexList(IEnumerable<T> set) {
            foreach (T item in set)
                Add(item);
        }

        public virtual int Add(T item) {
            int index = Data.Count;
            Data.Add(Tuple.Create(true, item));
            return index;
        }

        public int IndexOf(T item) {
            for (int i = 0; i < Data.Count; i++) {
                if (Data[i].Item2.Equals(item)) {
                    return i;
                }
            }
            return -1;
        }

        public void Remove(T item) {
            Remove(IndexOf(item));
        }

        public virtual void Remove(int index) {
            Data[index] = Tuple.Create<bool, T>(false, default(T));
        }

        public IEnumerator<T> GetEnumerator() {
            int i = 0;
            while (i < Data.Count) {
                if (Data[i].Item1 == true)
                    yield return Data[i].Item2;
                i++;
            }
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
            return GetEnumerator();
        }
    }
}